// pages/citylist/citylist.js
const citymap = require('../../libs/map')
var qqmapsdk = getApp().globalData.qqmapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cityName:'加载中',
    citymap:{},
    currentid:'',
    locOK:false
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadLocation()
    this.setData({
      citymap
    })
  },
  changeItemIndex(e){
    let {letter} = e.currentTarget.dataset
    this.setData({
      currentid:letter
    })
    console.log(this.data.currentid);
  },
  tapcityName(e){
    let {cityname} = e.currentTarget.dataset
    let ec = this.getOpenerEventChannel();
    ec.emit('acceptCityName',cityname)
    getApp().globalData.selectedCity = cityname
    wx.navigateBack({
      delta: -1,
    })
  },
  onShow(){
    // let pages = getCurrentPages()
    // let {ctname} = pages[pages.length-1].options
    // // console.log(ctname)
    // this.setData({
    //   ctname
    // })
  },
  loadLocation() {
    qqmapsdk.reverseGeocoder({
      success:res=>{console.log(res)
        this.setData({
          locOK:true,
          cityName:res.result.address_component.city
        })
        console.log(res)
      },
      fail:(err)=>{
        console.log(err)
        if(err.status==1000){
          wx.showModal({
            title:'提示',
            content:'定位服务需要授权,是否跳转到授权的页面',
            success:(res)=>{
              if(res.confirm){
                wx.openSetting({
                  success:settingRes=>{
                    console.log(settingRes)
                    if(settingRes.authSetting['scope.userLocation']){
                      this.loadLocation();
                    }
                  }
                })
              }else if(res.cancel){

              }
            }
          })
        }
        this.setData({
          ctname:'加载失败'
        })
      }
    })
  },
  tapCurrentCity(){
    if(this.data.locOK){
      let ec = this.getOpenerEventChannel();
      ec.emit('acceptCityName',this.data.cityName)
      wx.navigateBack({
        delta: -1,
      })
    }else{

    }
    this.loadLocation()
  },
})